package com.sceneworker.monitorpopup.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextArea;

import net.miginfocom.swing.MigLayout;

import com.myjavautils.swing.MigLayoutUtils;

public class SimpleYesNoDlg extends JDialog implements ActionListener
{
	public		JButton					mcl_ok;
	public		JButton					mcl_cancel;
	public		YesNoListener			mcl_par = null;
	
	public	SimpleYesNoDlg(JFrame cl_par, String str_message, int i_w, int i_h, YesNoListener cl_l) {
		this(cl_par, i_w, i_h, "Ok", str_message, "Cancel", cl_l);
	}
	
	public	SimpleYesNoDlg(JFrame cl_par, String str_message, int i_w, int i_h) {
		this(cl_par, i_w, i_h, "Ok", str_message, "Cancel", null);
	}
	
	public	SimpleYesNoDlg(JFrame cl_par, int i_w, int i_h, String str_yesText, String str_message, String str_noText, YesNoListener cl_l) {
		super(cl_par);
		
		mcl_par = cl_l;
		
		setSize(i_w, i_h);
		setLocationRelativeTo(null);
		setLayout(new MigLayout("inset 5"));
		
		if(str_message != null) {
			JTextArea		lcl_msg = new JTextArea();
			lcl_msg.setLineWrap(true);
			lcl_msg.setEditable(false);
			lcl_msg.setText(str_message);
			add(lcl_msg, "wrap, width 100%, height 100%, grow, span");
		}
		
		mcl_ok = new JButton(str_yesText);
		mcl_ok.addActionListener(this);
		
		if(str_noText != null) {
			mcl_cancel = new JButton(str_noText);
			mcl_cancel.addActionListener(this);
		}
		
		MigLayoutUtils.makeRightAlignedButtonBar(this.getContentPane(), mcl_cancel, mcl_ok);
		setModal(true);
	}
	
	public	SimpleYesNoDlg(JFrame cl_par, int i_w, int i_h, String str_yesText, String str_message)	{
		this(cl_par, i_w, i_h, str_yesText, str_message, null, null);
	}
	
	public	void addYesNoListener(YesNoListener cl_l) {
		mcl_par = cl_l;
	}
	
	public	void actionPerformed(ActionEvent cl_e)	{
		if(cl_e.getSource() == mcl_ok) {
			dispose();
			
			if(mcl_par != null)	{
				mcl_par.dialogDone(this, true);
			}
		}
		
		if(cl_e.getSource() == mcl_cancel) {
			dispose();
			
			if(mcl_par != null)	{
				mcl_par.dialogDone(this, false);
			}
		}
	}
	
	public interface YesNoListener {
		void		dialogDone(SimpleYesNoDlg cl_dlg, boolean b_ok);
	}
}
